<?php

namespace maotool\office\xls;

use maotool\core\request\ExportParam;
use maotool\core\request\PageParam;
use maotool\log\Console;
use maotool\object\ObjUtil;
use maotool\util\ArrayUtil;

class Sheet
{

    /**
     * 标题
     * @var
     */
    public $title;
    protected $header;

    protected $data;

    protected $list;

    /**
     * 树数据
     *
     * 适用于具有层级关系的数据组织，如组织架构、分类目录等
     *
     * @var array
     */
    protected $tree;

    protected $table;

    /**
     * @return mixed
     */
    public function getHeader()
    {
        return $this->header;
    }

    /**
     * @param mixed $header
     */
    public function setHeader($header)
    {
        $this->header = $header;
    }

    /**
     * @return mixed
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param mixed $data
     */
    public function setData($data)
    {
        $this->data = $data;
    }

    /**
     * @return mixed
     */
    public function getTable()
    {
        return $this->table;
    }

    /**
     * @param mixed $table
     */
    public function setTable($table)
    {
        $this->table = $table;
    }

    /**
     * @return mixed
     */
    public function getList()
    {
        return $this->list;
    }

    /**
     * @param mixed $list
     */
    public function setList($list)
    {
        $this->list = $list;
    }

    /**
     * 获取树形数据
     *
     * @return array 树形数据数组
     */
    public function getTree()
    {
        return $this->tree;
    }

    /**
     * 设置树形数据
     *
     * @param array $tree 树形数据数组
     * @return void
     */
    public function setTree($tree)
    {
        $this->tree = $tree;
    }

    /**
     * @return mixed
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param mixed $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * 分页获取数据
     * @param $next
     * @param $exportParam ExportParam
     * @return void
     */
    public function pagination($next, $exportParam = null)
    {
        $isHasNextPage = true;
        if (ObjUtil::isNull($exportParam)) {
            $exportParam = new ExportParam();
        }
        $pageNo = $exportParam->getPageNo();
        $pageTotal = 0;
        while ($isHasNextPage) {
            $pageParam = new PageParam();
            $pageParam->setPageNo($pageNo);
            $pageParam->setPageSize($exportParam->getPageSize());
            $pageParam->setParam($exportParam->getParam());
            $exportResult = $next($pageParam);
            $this->setList(ArrayUtil::merge($this->getList(), $exportResult->getList()));
            $isHasNextPage = $exportResult->isHasNextPage();
            $pageTotal = $pageTotal + $exportResult->getSize();
            Console::debug("Xlsx 分页数据加载中 当前是第 " . $pageNo . " 分片 分片数据数量 " . $exportResult->getSize() . " 总数量:" . $pageTotal);
            if ($isHasNextPage) {
                ++$pageNo;
            }
        }
        Console::debug("Xlsx 分页数据加载完成 当前是第 " . $pageNo . " 分片  总数量:" . $pageTotal);
    }

}